home *** CD-ROM | disk | FTP | other *** search
- Path: bcfreenet.seflin.lib.fl.us!bcfreenet!z007400b
- From: z007400b@bcfreenet.seflin.lib.fl.us (Ralph Silverman)
- Newsgroups: comp.lang.c
- Subject: Re: ugly constants and header files
- Date: 23 Jan 1996 20:14:24 GMT
- Organization: SEFLIN Free-Net - Broward
- Message-ID: <4e3fj0$a1v@bcfreenet.seflin.lib.fl.us>
- References: <k607w0zpkP3U088yn@merlin.magic.mb.ca> <DKvIKs.ACz@hpqmoea.sqf.hp.com>
- NNTP-Posting-Host: bcfreenet.seflin.lib.fl.us
- X-Newsreader: TIN [version 1.2 PL2]
-
- Neil Gall (neilg@hpqt0319.sqf.hp.com) wrote:
- : David C. Wright wrote:
-
- : > Create a single header file, of the form:
-
- : [ stuff ]
-
- : > #ifdef GLOBALS
-
- : > int globalvar;
- : > anystruct_t *ptr1;
- : > enumvars_t evars;
-
- : > #else
-
- : > extern int globalvar;
- : > extern anystruct_t *ptr1;
- : > extern enumvars_t evars;
-
- : > #endif
-
- : [ stuff ]
-
- : > #include this file in any modules that use the #defines/variables/prototypes
- : > in question. In *one* of the modules, use;
-
- : > #define GLOBALS
-
- : NO NO NO NO NO!!! Aargh! This is *really* horrible. Firstly, you've
- : got two declarations for the same variables but the compiler isn't aware
- : of the fact - if you change one and not the other by mistake you'll spend
- : ages looking for that wierd bug. In short it's a maintenance nightmare.
- : Secondly, declaring variables in header files is generally considered to
- : be pretty poor style. I've seen this done so many times, sometimes
- : disguised with some macros to avoid the double declaration problem, like
- : this:
-
- : #ifdef GLOBALS
- : #define variable
- : #else
- : #define variable extern
- : #endif
-
- : variable int x; etc...
-
- : This is just as ugly, and doesn't get away from the fact that variables
- : are defined in header files, which (I'll say this again) is a pretty poor
- : coding style and can be error prone. What happens if you accidentally
- : defined GLOBALS in another file ? You'll get linker problems.
-
- : There is one simple, clean and effective way of doing this which will
- : make your compiler complain if you do anything silly and which avoids
- : all these problems:
-
- : Declare all the variables in a header file, say vars.h
-
- : extern int globalvar;
- : extern anystruct_t *ptr1;
- : ...etc
-
- : And define them all in a source file, say vars.c, but remember to include
- : vars.h. This way you still define the variable in two places, but the
- : compiler will barf if there is a mismatch.
-
- : #include "vars.h"
-
- : int globalvar = 1;
- : anystruct_t *ptr1 = NULL;
-
- : What all this boils down to is that C is absolutely pathetic at handling
- : multiple source files. In fact, since it relies on the preprocessor you
- : could argue that C has no support at all for multiple source files.
- : Since using variables across multiple files is not very well supported
- : in the language, all sorts of wierd and wonderful schemes have been
- : designed to do it. In my mind, what to think about when doing this is
- : keeping the interface and the implementation of your modules separate.
- : Keep the interface in .h files and the implementation in .c files. This
- : fits neatly into C's limitations.
-
- : What I want to see in the next standard is a better way of exporting
- : module interfaces to other modules which can't be abused in the way that
- : header files tend to be.
-
- : --
- : | Neil Gall, Hewlett-Packard Ltd, Queensferry Telecoms Operation, |
- : | South Queensferry, Scotland +44 (0)131 331 7112 |
- : | neilg@hpsqf.sqf.hp.com http:/www.tardis.ed.ac.uk/~chrism/neil/nb/ |
- : | "That's what it'll be like in the future..." |
-
- --
- *********begin r.s. response**********
-
- what practical difference
- is there
- between
- .c
- and
- .h
- in this?
-
- *********end r.s. response************
- Ralph Silverman
- z007400b@bcfreenet.seflin.lib.fl.us
-
-